//
//  KFDICCommentViewController.m
//  KFD
//
//  Created by JIRENTIANXIANG on 7/8/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDICCommentViewController.h"
#import "UIView+KeyboardObserver.h"
#import "ui_config.h"
#import "KFDInvestmentCommunityManager.h"
#import "JTToast.h"

@interface KFDICCommentViewController ()

@property (weak, nonatomic) IBOutlet UIView *commentContainer;
@property (weak, nonatomic) IBOutlet UITextField *commentField;

@end

@implementation KFDICCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor=KFD_TRANSPARENT_COLOR;
    UITapGestureRecognizer* tapRecognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignCommentField)];
    [self.view addGestureRecognizer:tapRecognizer];
//    [self.commentField becomeFirstResponder];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.commentContainer addKeyboardObserver];
}

-(void)resignCommentField
{
    [self.commentContainer removeKeyboardObserver];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)sendButtonPressed:(id)sender {
    NSString* comment=self.commentField.text;
    KFDInvestmentCommunityManager* manager=[KFDInvestmentCommunityManager sharedManager];
    if ([manager checkComment:comment illegalInputHandler:^(NSString *message) {
        [[JTToast makeText:message] showInView:self.view size:CGSizeMake(message.length*20, 40)];
    }]) {
        [manager commentPost:self.post.id userId:self.post.uid content:comment successHandler:^{
            [self dismissViewControllerAnimated:YES completion:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:KFDICCommentCompletionNotification object:nil];
        } failureHandler:^(KFDResponseBody *response) {
            
        }];
    }
}

@end
